package com.goldcn.order.service;

import com.goldcn.common.CommonTools;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.config.Constants;
import com.goldcn.goods.dao.GoodsClassifyCommissionMapper;
import com.goldcn.goods.dao.GoodsClassifyRebateMapper;
import com.goldcn.goods.dao.GoodsClassifyRuleMapper;
import com.goldcn.goods.dao.GoodsInfoMapper;
import com.goldcn.goods.model.*;
import com.goldcn.goods.service.GoodsService;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.order.dao.OrderInfoMapper;
import com.goldcn.order.dao.OrderItemMapper;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/6.
 */
@Service
@Transactional
@ConfigurationProperties("bank")
public class RebateService {
    private String classifyUrl;
    private String classifySearchByIdUrl;

    public final static String SHOP_COMMISSION_RATE = "order.commission.rate";

    @Autowired
    GoodsClassifyRuleMapper goodsClassifyRuleMapper;//中收类型

    @Autowired
    GoodsClassifyRebateMapper goodsClassifyRebateMapper;//供应商分类中收规则
    @Autowired
    GoodsClassifyCommissionMapper goodsClassifyCommissionMapper;//平台分类中收规则

    @Autowired
    GoodsInfoMapper goodsInfoMapper;//按商品

    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    GoodsService goodsService;

    @Autowired
    RestTemplate rest;
    @Autowired
    RestTemplate restTemplate;

    public String getClassifyUrl() {
        return classifyUrl;
    }

    public void setClassifyUrl(String classifyUrl) {
        this.classifyUrl = classifyUrl;
    }

    public String getClassifySearchByIdUrl() {
        return classifySearchByIdUrl;
    }

    public void setClassifySearchByIdUrl(String classifySearchByIdUrl) {
        this.classifySearchByIdUrl = classifySearchByIdUrl;
    }

    /**
     * 查询中收规则
     *
     * @param companyID
     * @return
     */
    public GoodsClassifyRule selectByCompanyID(Long companyID) {
        return goodsClassifyRuleMapper.selectByCompanyID(companyID);
    }

    public boolean isGoods() {
        GoodsClassifyRule goodsClassifyRule = goodsClassifyRuleMapper.selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return false;
        }
        return Objects.equal(goodsClassifyRule.getRuleType(), Constants.TWO);
    }

    //是否是平台分类
    public boolean isPlatformClassify() {
        GoodsClassifyRule goodsClassifyRule = goodsClassifyRuleMapper.selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return false;
        }
        return Objects.equal(goodsClassifyRule.getRuleObj(), Constants.ONE) && Objects.equal(goodsClassifyRule.getRuleType(), Constants.ONE);
    }

    //是否是供应商分类
    public boolean isGysClassify() {
        GoodsClassifyRule goodsClassifyRule = goodsClassifyRuleMapper.selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return false;
        }
        return Objects.equal(goodsClassifyRule.getRuleObj(), Constants.TWO) && Objects.equal(goodsClassifyRule.getRuleType(), Constants.ONE);
    }

    //是否是平台商品
    public boolean isPlatformGoods() {
        GoodsClassifyRule goodsClassifyRule = goodsClassifyRuleMapper.selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return false;
        }
        return Objects.equal(goodsClassifyRule.getRuleObj(), Constants.ONE) && Objects.equal(goodsClassifyRule.getRuleType(), Constants.TWO);
    }

    //是否是供应商商品
    public boolean isGysGoods() {
        GoodsClassifyRule goodsClassifyRule = goodsClassifyRuleMapper.selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return false;
        }
        return Objects.equal(goodsClassifyRule.getRuleObj(), Constants.TWO) && Objects.equal(goodsClassifyRule.getRuleType(), Constants.TWO);
    }

    /**
     * 查询供应商分类和对应的中收规则
     *
     * @param sellerID
     * @return
     */
    public Object getClassListLevel(Long sellerID) {
        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            goodsClassifyRule = new GoodsClassifyRule();
            goodsClassifyRule.setRuleID(IdGen.get().nextId());
            goodsClassifyRule.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyRule.setRuleObj(Constants.ONE);
            goodsClassifyRule.setRuleType(Constants.ONE);
            goodsClassifyRuleMapper.insertSelective(goodsClassifyRule);

        }
        if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
            if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台
                if (sellerID > 0) {
                    //return Lists.newArrayList();
                    return goodsService.getClassList(sellerID);
                }
                return goodsService.getClassList(SystemContext.getCompanyID());
            } else {
                if (sellerID == 0) {
                    return Lists.newArrayList();
                }
                return goodsService.getClassList(sellerID);
            }
        }
        return Lists.newArrayList();
    }

    private boolean isFirstClassfys(String clazzId) {
        if (Strings.isNullOrEmpty(clazzId)) {
            return false;
        }
        List<String> list = getFirstClassfys();
        for (String str : list) {
            if (Objects.equal(str, clazzId)) {
                return true;
            }
        }
        return false;
    }

    public List<String> getFirstClassfys() {
        JSONObject jsonObject = rest.getForObject(classifyUrl + CommonTools.getExtString(restTemplate, "classifyType"), JSONObject.class);
        if (jsonObject.getInt("isSuccess") != 1) {
            throw new ServiceException("调用云管控服务出错", RestConst.ErrorCode.SERVICE_FAIL);
        }
        List<JSONObject> objectList = jsonObject.getJSONArray("data");
        List<String> classifyList = new ArrayList<String>();

        for (JSONObject jsonObject1 : objectList) {
            classifyList.add(jsonObject1.get("clazzid") + "");
        }
        return classifyList;
    }

    public List<String> getSubClassfy(String clazzId) {
        if (Strings.isNullOrEmpty(clazzId)) {
            return Lists.newArrayList();
        }
        JSONObject jsonObject = rest.getForObject(classifySearchByIdUrl + clazzId, JSONObject.class);
        if (jsonObject.getInt("isSuccess") != 1) {
            throw new ServiceException("调用云管控服务出错", RestConst.ErrorCode.SERVICE_FAIL);
        }
        List<JSONObject> objectList = jsonObject.getJSONArray("data");
        List<String> classifyList = new ArrayList<String>();

        for (JSONObject jsonObject1 : objectList) {
            classifyList.add(jsonObject1.get("clazzid") + "");
        }
        return classifyList;
    }

    /**
     * 查询供应商分类和对应的中收规则
     *
     * @param sellerID
     * @return
     */
    public Object getSubClassListLevel(Long sellerID, String clazzId) {
        if (sellerID == null || 0l == sellerID) {
            sellerID = SystemContext.getCompanyID();
        }
        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            goodsClassifyRule = new GoodsClassifyRule();
            goodsClassifyRule.setRuleID(IdGen.get().nextId());
            goodsClassifyRule.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyRule.setRuleObj(Constants.ONE);
            goodsClassifyRule.setRuleType(Constants.ONE);
            goodsClassifyRuleMapper.insertSelective(goodsClassifyRule);
            //return Lists.newArrayList();
        }
        if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
            return goodsService.getClassifyListById(clazzId, sellerID);

//            if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台
//                return goodsService.getClassifyListById(clazzId,sellerID);
//            } else {
//                return goodsService.getClassifyListById(clazzId, sellerID);
//            }
        }
        return Lists.newArrayList();
    }


    public Map<String, Object> selectByClazzIDList(List<String> clazzIDs, Long sellerID) {
        Map<String, Object> map = Maps.newHashMap();

        map.put("list", clazzIDs);
        map.put("sellerID", sellerID);//通过分类ID查询不需要公司ID，云管控传递，也获取不到
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (clazzIDs == null || clazzIDs.isEmpty()) {
            return resultMap;
        }
        List<GoodsClassifyRebate> list = goodsClassifyRebateMapper.selectByClazzIDList(map);
        for (GoodsClassifyRebate goodsClassifyRebate : list) {
            resultMap.put(goodsClassifyRebate.getClazzID(), goodsClassifyRebate);
        }
        return resultMap;
    }

    /**
     * 设置规则
     *
     * @param map ruleType  1按平台,2按供应商
     *            rangeTYpe 1按分类，2按商品
     */
    public int saveRule(Map<String, String> map) {
        Integer ruleType = MapUtils.getInteger(map, "ruleType");
        Integer rangeTYpe = MapUtils.getInteger(map, "rangeTYpe");
        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            goodsClassifyRule = new GoodsClassifyRule();
            goodsClassifyRule.setCompanyID(SystemContext.getCompanyID());
        }
        goodsClassifyRule.setRuleObj(ruleType);
        goodsClassifyRule.setRuleType(rangeTYpe);

        copyPlatformClassifyToGoods(rangeTYpe);//商品中收更新

        if (goodsClassifyRule.getRuleID() == null) {
            goodsClassifyRule.setRuleID(IdGen.get().nextId());
            return goodsClassifyRuleMapper.insertSelective(goodsClassifyRule);
        }
        return goodsClassifyRuleMapper.updateByPrimaryKey(goodsClassifyRule);
    }

    /**
     * 切换到商品更新商品的中收值
     */
    private void copyPlatformClassifyToGoods(Integer rangeTYpe) {
        if (!Objects.equal(rangeTYpe, Constants.TWO)) {
            return;
        }
        List<GoodsClassifyCommission> list = goodsClassifyCommissionMapper.selectAllByCompanyID(SystemContext.getCompanyID());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<GoodsInfo> goodsList = goodsInfoMapper.selectAllByCompanyID(SystemContext.getCompanyID());
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }
        for (GoodsInfo goodsInfo : goodsList) {
            goodsInfoMapper.updateVal(getFromPlatformClassify(goodsInfo, list));
        }
    }

    private GoodsInfo getFromPlatformClassify(GoodsInfo goodsInfo, List<GoodsClassifyCommission> list) {
        if (goodsInfo.getBankVal() != null && goodsInfo.getEmployeeVal() != null) {
            return goodsInfo;
        }
        String subClassifyID = goodsInfo.getSubClassifyID();
        GoodsClassifyCommission subClassifyCommission = null;
        for (GoodsClassifyCommission goodsClassifyCommission : list) {
            if (Objects.equal(goodsClassifyCommission.getClazzID(), subClassifyID)) {
                subClassifyCommission = goodsClassifyCommission;
                break;
            }
        }
        if (subClassifyCommission != null) {
            if (goodsInfo.getBankVal() == null) {
                goodsInfo.setBankVal(subClassifyCommission.getVal());
            }
            if (goodsInfo.getEmployeeVal() == null) {
                goodsInfo.setEmployeeVal(subClassifyCommission.getEmployeeVal());
            }
        }

        return goodsInfo;
    }

    /**
     * 设置中收规则
     *
     * @param map
     */
    public int saveRebateVal(Map<String, String> map) {
        String clazzID = MapUtils.getString(map, "clazzID", "");
        Integer bankVal = MapUtils.getInteger(map, "bankVal", 0);
        Integer employeeVal = MapUtils.getInteger(map, "employeeVal", 0);
        Integer thirdVal = MapUtils.getInteger(map, "thirdVal", 0);
        Integer deductionRate = MapUtils.getInteger(map, "deductionRate", 0);
        //商城ID或者平台id
        Long sellerID = Objects.equal(MapUtils.getLong(map, "sellerID", 0l), 0L) ? SystemContext.getCompanyID() : MapUtils.getLong(map, "sellerID");

        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            throw new ServiceException("请先设置中收规则", RestConst.ErrorCode.SERVICE_FAIL);
        }
        if (goodsClassifyRule.getRuleObj() == null || goodsClassifyRule.getRuleObj() > 2 || goodsClassifyRule.getRuleObj() < 1) {
            throw new ServiceException("请先进行规则设定", RestConst.ErrorCode.SERVICE_FAIL);
        }
        if (goodsClassifyRule.getRuleType() == null || goodsClassifyRule.getRuleType() > 2 || goodsClassifyRule.getRuleType() < 1) {
            throw new ServiceException("请先进行范围设定", RestConst.ErrorCode.SERVICE_FAIL);
        }

        if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                saveClassifyVal(clazzID, bankVal, employeeVal, thirdVal, deductionRate,goodsClassifyRule.getRuleObj(), sellerID);
            } else {//商品
                throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
            }
        } else {//供应商
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                if (sellerID == null || sellerID == 0) {
                    throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
                }
                saveClassifyVal(clazzID, bankVal, employeeVal, thirdVal, deductionRate,goodsClassifyRule.getRuleObj(), sellerID);
            } else {//商品
                throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
            }
        }
        return 1;
    }


    /**
     * 设置中收规则
     *
     * @param map
     */
    public int saveRebateVal_v2(Map<String, String> map) {
        String clazzID = MapUtils.getString(map, "clazzID", "");
        Integer bankVal = MapUtils.getInteger(map, "bankVal", 0);
        Integer employeeVal = MapUtils.getInteger(map, "employeeVal", 0);
        Integer thirdVal = MapUtils.getInteger(map, "thirdVal", 0);
        Integer deductionRate = MapUtils.getInteger(map, "deductionRate", 0);
        //商城ID或者平台id
        Long sellerID = Objects.equal(MapUtils.getLong(map, "sellerID", 0l), 0L) ? SystemContext.getCompanyID() : MapUtils.getLong(map, "sellerID");
        Integer type = MapUtils.getInteger(map, "type", 1);


        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            throw new ServiceException("请先设置中收规则", RestConst.ErrorCode.SERVICE_FAIL);
        }
        if (goodsClassifyRule.getRuleObj() == null || goodsClassifyRule.getRuleObj() > 2 || goodsClassifyRule.getRuleObj() < 1) {
            throw new ServiceException("请先进行规则设定", RestConst.ErrorCode.SERVICE_FAIL);
        }
        if (goodsClassifyRule.getRuleType() == null || goodsClassifyRule.getRuleType() > 2 || goodsClassifyRule.getRuleType() < 1) {
            throw new ServiceException("请先进行范围设定", RestConst.ErrorCode.SERVICE_FAIL);
        }

        if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                saveClassifyVal(clazzID, bankVal, employeeVal, thirdVal, deductionRate,type, sellerID);
            } else {//商品
                throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
            }
        } else {//供应商
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                if (sellerID == null || sellerID == 0) {
                    throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
                }
                saveClassifyVal(clazzID, bankVal, employeeVal, thirdVal, deductionRate,goodsClassifyRule.getRuleObj(), sellerID);
            } else {//商品
                throw new ServiceException("中收规则变更，操作失败", RestConst.ErrorCode.SERVICE_FAIL);
            }
        }
        return 1;
    }

    /**
     * 获取扣点值千分比
     *
     * @param firstClassify
     * @param subClassify
     * @param goodsID
     * @param sellerID
     * @return
     */
    public Map<String, Integer> getRebateVal(String firstClassify, String subClassify, Long goodsID, Long sellerID) {
        Map<String, Integer> resultMap = Maps.newHashMap();//
        GoodsClassifyRule goodsClassifyRule = selectByCompanyID(SystemContext.getCompanyID());
        if (goodsClassifyRule == null) {
            return getResult(0, 0);
        }
        if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                return getPlaVal(goodsClassifyRule, firstClassify, subClassify, sellerID);
            } else {//商品
                return getGysVal(goodsID);
            }
        } else {//供应商
            if (Objects.equal(goodsClassifyRule.getRuleType(), 1)) {//分类
                return getPlaVal(goodsClassifyRule, firstClassify, subClassify, sellerID);
            } else {//商品
                return getGysVal(goodsID);
            }
        }
    }

    /**
     * 获取分类扣点千分比
     *
     * @param goodsClassifyRule
     * @param firstClassify
     * @param subClassify
     * @param sellerID
     * @return
     */
    private Map<String, Integer> getPlaVal(GoodsClassifyRule goodsClassifyRule, String firstClassify, String subClassify, Long sellerID) {

        Long companyID = SystemContext.getCompanyID();
        if (Objects.equal(goodsClassifyRule.getRuleObj(), 1)) {//平台分类
            GoodsClassifyCommission firstGoodsClassifyCommission = null;
            GoodsClassifyCommission subGoodsClassifyCommission = null;

            Integer bankVal = 0;
            Integer employeeVal = 0;
            if (!Strings.isNullOrEmpty(subClassify)) {
                subGoodsClassifyCommission = goodsClassifyCommissionMapper.getValByClassID(subClassify, companyID);
            }
            if (subGoodsClassifyCommission != null) {
                if (subGoodsClassifyCommission.getVal() != null) {
                    bankVal = subGoodsClassifyCommission.getVal();
                }
                if (subGoodsClassifyCommission.getEmployeeVal() != null) {
                    employeeVal = subGoodsClassifyCommission.getEmployeeVal();
                }
            }

            return getResult(bankVal, employeeVal);
        } else {//供应商分类
            GoodsClassifyRebate firstGoodsClassifyRebate = null;
            GoodsClassifyRebate subGoodsClassifyRebate = null;
            Integer bankVal = 0;
            Integer employeeVal = 0;
            if (!Strings.isNullOrEmpty(subClassify)) {
                subGoodsClassifyRebate = goodsClassifyRebateMapper.selectByClazzID(subClassify, sellerID);
            }
            if (subGoodsClassifyRebate != null) {
                if (subGoodsClassifyRebate.getBankVal() != null) {
                    bankVal = subGoodsClassifyRebate.getBankVal();
                }
                if (subGoodsClassifyRebate.getEmployeeVal() != null) {
                    employeeVal = subGoodsClassifyRebate.getEmployeeVal();
                }
            }

            return getResult(bankVal, employeeVal);
        }
    }


    private Map<String, Integer> getGysVal(Long goodsID) {
        GoodsInfo goodsInfo = goodsInfoMapper.selectByPrimaryKey(goodsID);
        Integer bankVal = 0;
        Integer employeeVal = 0;
        if (goodsInfo != null) {
            if (goodsInfo.getBankVal() != null) {
                bankVal = goodsInfo.getBankVal();
            }
            if (goodsInfo.getEmployeeVal() != null) {
                employeeVal = goodsInfo.getEmployeeVal();
            }
        }
        return getResult(bankVal, employeeVal);
    }

    private Map<String, Integer> getResult(Integer bankVal, Integer employeeVal) {
        Map<String, Integer> resultMap = Maps.newHashMap();//
        resultMap.put(Constants.bankVal, bankVal);
        resultMap.put(Constants.employeeVal, employeeVal);
        return resultMap;
    }


    private void saveClassifyVal(String clazzID, Integer bankVal, Integer employeeVal, Integer thirdVal,Integer deductionRate, Integer type, Long sellerID) {
        if (Strings.isNullOrEmpty(clazzID)) {
            throw new ServiceException("未获取到分类", RestConst.ErrorCode.SERVICE_FAIL);
        }
        saveGysClassifyVal(clazzID, bankVal, employeeVal, thirdVal, deductionRate,sellerID, type);

    }

    private void savePlaClassifyVal(String clazzID, Integer bankVal, Integer employeeVal) {
        GoodsClassifyCommission goodsClassifyCommission = goodsClassifyCommissionMapper.selectByClazzID(clazzID, SystemContext.getCompanyID());
        if (goodsClassifyCommission == null) {
            goodsClassifyCommission = new GoodsClassifyCommission();
            goodsClassifyCommission.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyCommission.setClazzID(clazzID);
        }
        goodsClassifyCommission.setVal(bankVal);
        goodsClassifyCommission.setEmployeeVal(employeeVal);

        if (goodsClassifyCommission.getID() == null) {
            goodsClassifyCommission.setID(IdGen.get().nextId());
            goodsClassifyCommissionMapper.insertSelective(goodsClassifyCommission);
        } else {
            goodsClassifyCommissionMapper.updateByPrimaryKeySelective(goodsClassifyCommission);
        }
        //判断是一级
        if (isFirstClassfys(clazzID)) {
            List<String> subList = getSubClassfy(clazzID);
            for (String subStr : subList) {
                savePlaClassifyVal2(subStr, bankVal, employeeVal);
            }
        }
    }

    private void savePlaClassifyVal2(String clazzID, Integer bankVal, Integer employeeVal) {
        GoodsClassifyCommission goodsClassifyCommission = goodsClassifyCommissionMapper.selectByClazzID(clazzID, SystemContext.getCompanyID());
        if (goodsClassifyCommission == null) {
            goodsClassifyCommission = new GoodsClassifyCommission();
            goodsClassifyCommission.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyCommission.setClazzID(clazzID);
        }
        goodsClassifyCommission.setVal(bankVal);
        goodsClassifyCommission.setEmployeeVal(employeeVal);

        if (goodsClassifyCommission.getID() == null) {
            goodsClassifyCommission.setID(IdGen.get().nextId());
            goodsClassifyCommissionMapper.insertSelective(goodsClassifyCommission);
        } else {
            goodsClassifyCommissionMapper.updateByPrimaryKeySelective(goodsClassifyCommission);
        }
    }


    private void saveGysClassifyVal(String clazzID, Integer bankVal, Integer employeeVal, Integer thirdVal,Integer deductionRate, Long sellerID, Integer type) {
        if (sellerID == null) {
            throw new ServiceException("未获取到供应商信息", RestConst.ErrorCode.SERVICE_FAIL);
        }
        GoodsClassifyRebate goodsClassifyRebate = goodsClassifyRebateMapper.selectByClazzID(clazzID, sellerID);
        if (goodsClassifyRebate == null) {
            goodsClassifyRebate = new GoodsClassifyRebate();
            goodsClassifyRebate.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyRebate.setClazzID(clazzID);
            goodsClassifyRebate.setType(type);
            goodsClassifyRebate.setSellerID(sellerID);
        }
        goodsClassifyRebate.setBankVal(bankVal);
        goodsClassifyRebate.setEmployeeVal(employeeVal);
        goodsClassifyRebate.setThirdVal(thirdVal);
        goodsClassifyRebate.setDeductionRate(deductionRate);
        goodsClassifyRebate.setUpdateTime(System.currentTimeMillis());

        if (goodsClassifyRebate.getRebateID() == null) {
            goodsClassifyRebate.setRebateID(IdGen.get().nextId());
            goodsClassifyRebateMapper.insertSelective(goodsClassifyRebate);
        } else {
            goodsClassifyRebateMapper.updateByPrimaryKeySelective(goodsClassifyRebate);
        }
        //判断是一级
        if (isFirstClassfys(clazzID)) {
            List<String> subList = getSubClassfy(clazzID);
            for (String subStr : subList) {
                saveGysClassifyVal2(subStr, bankVal, employeeVal, thirdVal,deductionRate, sellerID, type);
            }
        }
    }

    private int saveGysClassifyVal2(String clazzID, Integer bankVal, Integer employeeVal, Integer thirdVal,Integer deductionRate, Long sellerID, Integer type) {
        if (sellerID == null) {
            throw new ServiceException("未获取到供应商信息", RestConst.ErrorCode.SERVICE_FAIL);
        }
        GoodsClassifyRebate goodsClassifyRebate = goodsClassifyRebateMapper.selectByClazzID(clazzID, sellerID);
        if (goodsClassifyRebate == null) {
            goodsClassifyRebate = new GoodsClassifyRebate();
            goodsClassifyRebate.setCompanyID(SystemContext.getCompanyID());
            goodsClassifyRebate.setClazzID(clazzID);
            goodsClassifyRebate.setType(type);
            goodsClassifyRebate.setSellerID(sellerID);
        }
        goodsClassifyRebate.setBankVal(bankVal);
        goodsClassifyRebate.setEmployeeVal(employeeVal);
        goodsClassifyRebate.setThirdVal(thirdVal);
        goodsClassifyRebate.setDeductionRate(deductionRate);
        goodsClassifyRebate.setUpdateTime(System.currentTimeMillis());

        if (goodsClassifyRebate.getRebateID() == null) {
            goodsClassifyRebate.setRebateID(IdGen.get().nextId());
            return goodsClassifyRebateMapper.insertSelective(goodsClassifyRebate);
        }
        return goodsClassifyRebateMapper.updateByPrimaryKeySelective(goodsClassifyRebate);
    }

    //平台审核商品，供应商添加商品
    private int saveGoodsVal(Long goodID, Integer bankVal, Integer employeeVal) {
        GoodsInfoWithBLOBs goodsInfo = goodsInfoMapper.selectByPrimaryKey(goodID);
        if (goodsInfo == null) {
            throw new ServiceException("未获取到商品信息", RestConst.ErrorCode.SERVICE_FAIL);
        }
        goodsInfo.setBankVal(bankVal);
        goodsInfo.setEmployeeVal(employeeVal);
        return goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
    }


    public Object selectListByShopID(String name, Long sellerID) {
        GoodsClassifyRebate goodsClassifyRebate = goodsClassifyRebateMapper.selectByClazzID(name, sellerID);
        return goodsClassifyRebate;
    }


    public GoodsClassifyRebate selectDefaultRateByShopID(Long sellerID) {
        GoodsClassifyRebate goodsClassifyRebate = goodsClassifyRebateMapper.selectByClazzID(SHOP_COMMISSION_RATE, sellerID);
        if (goodsClassifyRebate == null) {
            return goodsClassifyRebateMapper.selectByClazzID(SHOP_COMMISSION_RATE, SystemContext.getCompanyID());
        }
        return goodsClassifyRebate;
    }


    public List<GoodsClassifyRebate> selectListBySellID(Long sellerID, Integer type) {
        return goodsClassifyRebateMapper.selectBySellID(sellerID, type);
    }

    public Object saveShopData(GoodsClassifyRebate shopRebate) {

        int i = goodsClassifyRebateMapper.updateByPrimaryKeySelective(shopRebate);
        if (i == 0) {
            shopRebate.setType(2);
            shopRebate.setUpdateTime(CommonUtils.getCurTimestamp());
            shopRebate.setRebateID(IdGen.get().nextId());
            shopRebate.setCompanyID(SystemContext.getCompanyID());
            i = goodsClassifyRebateMapper.insertSelective(shopRebate);
        }

        return i;

    }

}
